﻿using _9jCardRegisterCollection.Models;
using AntdUI;
using CardRegisterCollection.BLL;
using CardRegisterCollection.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _9jCardRegisterCollection.Views
{
    public partial class CarPage : UserControl
    {
        public CarPage(AntdUI.Window _window)
        {
            window = _window;
            InitializeComponent();

            //初始化表格列头
            InitTableColumns();
            InitData();
        }

        AntdUI.Window window;
        AntList<CarExpand> car;
        private readonly CardRegisterCollection.BLL.Car _carBll = new CardRegisterCollection.BLL.Car();
        private void InitTableColumns()
        {
            tabCar.Columns = new ColumnCollection() {
                new ColumnCheck("Selected"){Fixed = true},
                new Column("Id", "ID", ColumnAlign.Center){Visible = false,},
                new Column("CarNumber", "车牌号", ColumnAlign.Center),
                new Column("DriverName", "驾驶员姓名", ColumnAlign.Center) ,
                new Column("DriverIDNumber", "驾驶员证件号码", ColumnAlign.Center),
                new Column("Telephone", "驾驶员手机号",ColumnAlign.Center),
                new Column("CardType", "申请证件类型",ColumnAlign.Center),
                new Column("UseUnit", "使用单位",ColumnAlign.Center),
                new Column("Remark", "备注一",ColumnAlign.Center),
                new Column("CreatedBy", "创建人",ColumnAlign.Center),
                new Column("CreatedAt", "创建时间",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center)
                {
                    Fixed = true,//冻结列
                }
            };
        }

        private void InitData()
        {
            car = new AntList<CarExpand>();

            var carList = new List<CarExpand>();

            var ds = _carBll.GetList("");
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    carList.Add(new CarExpand
                    {
                        ID = row["ID"]?.ToString(),
                        CarNumber = row["CarNumber"]?.ToString(),
                        DriverName = row["DriverName"]?.ToString(),
                        DriverIDNumber = row["DriverIDNumber"]?.ToString(),
                        Telephone = row["Telephone"]?.ToString(),
                        UseUnit = row["UseUnit"]?.ToString(),
                        CardType = row["CardType"]?.ToString(),
                        Remark = row["Remark"]?.ToString(),
                        CreatedBy = row.IsNull("CreatedBy") ? string.Empty : row["CreatedBy"].ToString(),
                        CreatedAt = row.IsNull("CreatedAt") ? (DateTime?)null : Convert.ToDateTime(row["CreatedAt"]),
                    });
                }
            }
            car = new AntList<CarExpand>(carList);
            tabCar.Binding(car);
        }

        private void butSearch_Click(object sender, EventArgs e)
        {
            InitData();
        }

        private void tabCar_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var data = (CarExpand)e.Record;
            if (data == null) return;

            var id = e.Btn.Id;
            switch (id)
            {
                case "Edit":
                    {
                        var form = new CarEdit(window, data);
                        Modal.open(new AntdUI.Modal.Config(window, "修改", form, TType.None)
                        {
                            BtnHeight = 0,
                        }); ;
                        if (form.Submit)
                        {
                            _carBll.Update(data);
                            InitData();
                        }
                    }
                    break;
                case "Del":
                    {
                        DelById(data);
                    }
                    break;
            }
        }

        private void buttonADD_Click(object sender, EventArgs e)
        {
            CardRegisterCollection.Model.Car car = new CardRegisterCollection.Model.Car();
            var form = new CarEdit(window, car) { Size = new Size(500, 300) };
            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
            {
                OnLoad = () =>
                {
                    AntdUI.Message.info(window, "开始添加", autoClose: 1);
                    
                },
                OnClose = () =>
                {
                    AntdUI.Message.info(window, "结束添加", autoClose: 1);
                    if (form.SureToChange)
                    {
                       if (_carBll.Add(car))
                        {
                            AntdUI.Message.success(window, "添加成功", autoClose: 1);
                            InitData();
                        }
                    }
                }
            });
           
        }

        private void DelById(CarExpand car)
        {
            _carBll.Delete(car.ID);
            InitData();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            CarImportExcel form = new CarImportExcel(window);
            form.ShowDialog();
            InitData();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            CarExportExcel form = new CarExportExcel(window, car);
            form.ShowDialog();
            InitData();
        }
    }
}
